package com.sgb.member.ui.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.member.R;
import com.sgb.member.entity.Record;

import java.util.ArrayList;

public class MemberDoorAdapter2 extends RecyclerView.Adapter<MemberDoorAdapter2.ViewHolder> {
    private ArrayList<Record> mData;
    private MemberDoorAdapter2.OnItemClickListener onItemClickListener;

    public MemberDoorAdapter2(ArrayList<Record> data) {
        this.mData = data;
    }

    public void setOnItemClickListener(MemberDoorAdapter2.OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_member_door, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull MemberDoorAdapter2.ViewHolder holder, int position) {
        Record record = mData.get(position);
        String relation = record.getRelation();
        if (relation.equals("1")) {
            holder.tv_member_tip.setText("供方会员");
            holder.tv_member_tip.setBackgroundResource(R.drawable.corner2_9f55e9_be85ff_bg);
        } else{
            holder.tv_member_tip.setText("需方会员");
            holder.tv_member_tip.setBackgroundResource(R.drawable.corner2_6b78ff_79a2ff_bg);
        }

        String title = record.getRequiredContent();
        holder.tv_title.setText(title);
        StringBuilder stringBuilder = new StringBuilder();
        if (!TextUtils.isEmpty(record.getFirstCategoryName())) {
            stringBuilder.append(record.getFirstCategoryName());
        }
        if (!TextUtils.isEmpty(record.getSecondCategoryName())) {
            stringBuilder.append("/");
            stringBuilder.append(record.getSecondCategoryName());
        }
        if (!TextUtils.isEmpty(record.getThirdCategoryName())) {
            stringBuilder.append("/");
            stringBuilder.append(record.getThirdCategoryName());
        }

        holder.tv_member_type.setText(stringBuilder.toString());
        holder.tv_file_.setText(record.isSupportDoc().equals("1") ?  "是": "否");
        holder.tv_edit.setOnClickListener(v -> {
            onItemClickListener.onItemClick(record,holder.tv_member_type.getText().toString());
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_member_tip, tv_title, tv_member_type, tv_file_, tv_edit;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_member_tip = itemView.findViewById(R.id.tv_member_tip);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_member_type = itemView.findViewById(R.id.tv_member_type);
            tv_file_ = itemView.findViewById(R.id.tv_file_);
            tv_edit = itemView.findViewById(R.id.tv_edit);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(Record record,String typeName);
    }
}
